package tests;

import common.Utils;
import org.openqa.selenium.By;

/**
 * 测试用户登陆
 */



public class Login extends Utils {

   public static String url = "http://110.41.148.165:13145/sign-in.html";

    /**
     * 执行完该方法，就会跳转到登陆页面
     */
    public Login() {
        super(url);
    }


    /**
     * 检查页面是否加载成功 -- 成功后才进行登陆测试
     */
    public void loginIsRight() {
        //1.查看《登陆》字样是否存在
        driver.findElement(By.cssSelector("body > div.page.page-center > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
        //2.查看登陆输入框是否加载出来
        driver.findElement(By.cssSelector("#username"));
    }




    /**
     * 登陆失败的场景 -- 账号错误
     */
    public void loginError1() {
        //1.清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //2.输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys("ljy666");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //3.验证是否登陆错误
        String expect = driver.getTitle();
        assert expect.equals("学识论坛-用户登陆");

    }

    /**
     * 登陆失败的场景 -- 密码错误
     */
    public void loginError2() {
        //1.清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //2.输入账号密码
        driver.findElement(By.cssSelector("#username")).sendKeys("ljy");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456789");
        driver.findElement(By.cssSelector("#submit")).click();

        //3.验证是否登陆错误
        String expect = driver.getTitle();
        assert expect.equals("学识论坛-用户登陆");

    }


    /**
     * 测试登陆成功
     */
    public void loginSuc() {

        //1.清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //2.输入账号密码并且登陆
        driver.findElement(By.cssSelector("#username")).sendKeys("ljy");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");

        driver.findElement(By.cssSelector("#submit")).click();

        //3.成功登陆后，检查页面
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post"));

        //
        String expect = driver.getTitle();
        //System.out.println("======"+expect);
        assert expect.equals("学识论坛");
        //回退，进行异常登陆的情况
        //driver.navigate().back();
    }

}
